package com.serene.authority.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.serene.authority.admin.entity.vo.DeptVo;
import com.serene.authority.admin.entity.vo.MenuVo;
import com.serene.authority.admin.entity.vo.MenuIndexVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author  serene
 * @createTime 2020/7/2
 */
public class TreeUtil {
    //todo 判断list是否为空

    /**
     *
     * @param listByRoleId 通过角色id查询的menuid
     * @param menuDtos 返回的menutree
     * @return
     */
    public static List<MenuVo> menutree(List<MenuVo> listByRoleId, List<MenuVo> menuDtos ){
        // if (listByRoleId == null & listByRoleId.size() ==0){
        //     throw
        // }
        List<Integer> collect = listByRoleId.stream().map(MenuVo::getId).collect(Collectors.toList());
        List<Integer> collect1 = menuDtos.stream().map(MenuVo::getId).collect(Collectors.toList());
        // 遍历list2
        for (Integer item : collect) {
            // 如果存在这个数
            if (collect1.contains(item)) {
                MenuVo menuDto = new MenuVo();
                int i = collect1.indexOf(item);
                menuDto = menuDtos.get(i);
                menuDto.setCheckArr("1");
                menuDtos.set(i,menuDto);
            }
        }
        return menuDtos;
    }

    public static List<DeptVo> deptTree(List<DeptVo> listById, List<DeptVo> lists ){
        // if (listByRoleId == null & listByRoleId.size() ==0){
        //     throw
        // }
        List<Integer> collect = listById.stream().map(DeptVo::getId).collect(Collectors.toList());
        List<Integer> collect1 = lists.stream().map(DeptVo::getId).collect(Collectors.toList());
        // 遍历list2
        for (Integer item : collect) {
            // 如果存在这个数
            if (collect1.contains(item)) {
                DeptVo deptDto = new DeptVo();
                int i = collect1.indexOf(item);
                deptDto = lists.get(i);
                deptDto.setCheckArr("1");
                lists.set(i,deptDto);
            }
        }
        return lists;
    }

    public static void setMenuTree(Integer parentId, List<MenuIndexVo> menusAll, JSONArray array) {
        for (MenuIndexVo per : menusAll) {
            if (per.getParentId().equals(parentId)) {
                String string = JSONObject.toJSONString(per);
                JSONObject parent = (JSONObject) JSONObject.parse(string);
                array.add(parent);
                if (menusAll.stream().filter(p -> p.getParentId().equals(per.getId())).findAny() != null) {
                    JSONArray child = new JSONArray();
                    parent.put("child", child);
                    setMenuTree(per.getId(), menusAll, child);
                }
            }
        }
    }

    public static List<MenuIndexVo> parseMenuTree(List<MenuIndexVo> list){
        List<MenuIndexVo> result = new ArrayList<MenuIndexVo>();
        // 1、获取第一级节点
        for (MenuIndexVo menu : list) {
            if(menu.getParentId() == 0) {
                result.add(menu);
            }
        }
        // 2、递归获取子节点
        for (MenuIndexVo parent : result) {
            parent = recursiveTree(parent, list);
        }
        return result;
    }

    public static MenuIndexVo recursiveTree(MenuIndexVo parent, List<MenuIndexVo> list) {
        List<MenuIndexVo>children = new ArrayList<>();
        for (MenuIndexVo menu : list) {
            if (Objects.equals(parent.getId(), menu.getParentId())) {
                children.add(menu);
            }
            parent.setChildren(children);
        }
        return parent;
    }
}
